home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9612 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: skivs.ski.org!usenet
  2. From: gt@ns.oon.or.jp (Gemini Thunder)
  3. Newsgroups: comp.lang.c
  4. Subject: problem with struct & qsort()???
  5. Date: Mon, 11 Mar 1996 16:07:54 GMT
  6. Organization: Smith-Kettlewell Eye Research Institute
  7. Message-ID: <4i1j7h$h8f@skivs.ski.org>
  8. NNTP-Posting-Host: oonserv.oon.or.jp
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. Greetings and Salutations.
  12. I am stuck on a bug in a little program.  I (think) have nailed it
  13. down, and am asking for your help.  I will try to summarize the
  14. problem without posting pages of code.
  15.  
  16. The purpose here is to take an array of structures, sort them, 
  17. save them to disk, and later read them.
  18. So I use this struct STUDENT (containing struct DATE):
  19.  
  20. /* DDMMMYY format */
  21. typedef struct d{
  22.   int d_day;
  23.   char d_month[4];
  24.   int d_year;
  25. } DATE;
  26.  
  27. typedef struct s{
  28.   int std_num;
  29.   char name[INPUT_SIZE + 1];
  30.   DATE dob,
  31.     *dptr;
  32. }STUDENT;
  33.  
  34. so later i get an array:
  35. STUDENT recarray[MAX_STUDENTS]; /* maybe 20 or so, whatever */
  36.  
  37. so i get the input, that is good, and i save to disk, that is good,
  38. and i read from disk and print out that is good.
  39. _BUT_ when i introduce a call to qsort() after i get all the structs,
  40. but before i save, the input is garbled.
  41.  
  42.  qsort( (void *)recarray, MAX_STUDENTS, sizeof(recarray[0]), sorter);
  43.  
  44. with sorter() being:
  45.  
  46. int sorter(const void *a, const void *b){
  47.   return( ( ((STUDENT *)a) -> std_num) -
  48.                ( ((STUDENT *)b) -> std_num) );
  49. }
  50.  
  51. anyway, as i say if i just comment out the call to qsort() everything
  52. goes well. i have (out of desperation) tried replacing MAX_STUDENTS in
  53. the qsort() call with the actual number of structs in array , to no
  54. avail.
  55.  
  56. also a breakpoint in a "debug" version of sorter() (using variables)
  57. reveals that the b -> std_num is incorrect, but a -> std_num seems
  58. okay.
  59.  
  60. i have ran the code through Clint, nothing turned up.  there are no
  61. errors nor warnings when i compile, with all options turned on (turbo
  62. C++ 3.0 / msdos)
  63.  
  64. i can find nothing in any book or online documentation i have on a
  65. special problem using structs with qsort() so i am forced to conclude
  66. i am an idiot.
  67.  
  68. any help would be greatly appreciated, or i can just post the entire
  69. source if i did not give enough information here.  thank you.
  70.  
  71.